<?php

class Search_modul  extends CI_Model {
	public $select;

	# id телефона
	public $id_phone;

	public function __construct() {
		// Call the Model constructor
		parent::__construct();
	}

	/**
	 * Поиск наглавной главная
	 *
	 * @return void
	 * @author
	 */
	public function adsent_count($type_adsent = false) {

		$time = false;

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		if ($type_adsent == false) {
			$select = "SELECT    COUNT(a.region_id) `sadsent`, r.*
				FROM   `adsent` AS `a` RIGHT  JOIN    `region` AS `r`  
				ON     a.region_id = r.code  
				GROUP BY r.code  
				ORDER BY r.sort ASC;";
		} else {

			$select = "SELECT    COUNT(a.region_id) `sadsent`, r.*
FROM   `adsent` AS `a` RIGHT  JOIN    `region` AS `r`   
ON     a.region_id = r.code 
AND a.name_type = '" . $type_adsent . "'
GROUP BY r.code  
ORDER BY r.sort ASC";
		}

		$qs = $DB2 -> query($select);

		if ($qs -> num_rows() > 0) {

			foreach ($qs -> result() as $row) {

				$time[] = array('sadsent' => $row -> sadsent, 'id' => $row -> id, 
				'name' => $row -> name, 'fullname' => $row -> fullname, 'socr' => $row -> socr, 
				'code' => $row -> code, );
			}

			return $time;
		}
		return 0;
	}

	/**
	 * Подсчёт колличества объявлений на заданом регионе.
	 *
	 * @return void
	 * @author
	 */
	public function adsent_type($type_adsent = false) {

		$time = false;

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		if ($type_adsent == false) {
			$select = "SELECT     COUNT(a.id) sum, a.name_type type  , r.code
						FROM    `adsent`  a join `region` r on r.code = a.region_id
						GROUP BY a.name_type;";
		} else {

			$select = "SELECT    COUNT(a.id) sum, a.name_type type , r.code 
						FROM   `adsent`  a join `region` r on r.code = a.region_id
						where r.id = '" . $type_adsent . "'
						GROUP BY a.name_type ";
		}

		$qs = $DB2 -> query($select);

		if ($qs -> num_rows() > 0) {

			foreach ($qs -> result() as $row) {

				$t[$row -> type] = $row -> sum;
				# 'type' => $row -> type ,
				$t['code'] = $row -> code;
			}

			return $t;
		}
		return 0;
	}

	/**
	 * Поиск региона по id
	 *
	 * @return void
	 * @author
	 */
	public function select_region($id = 77) {

		$time = false;

		$DB2 = $this -> load -> database('adsent', TRUE, TRUE);

		$select = "SELECT     r.* FROM   `region` r where r.id = " . $id . ";";

		$qs = $DB2 -> query($select);

		if ($qs -> num_rows() > 0) {

			foreach ($qs -> result() as $row) {

				$t['code'] = $row -> code;
				$t['socr'] = $row -> socr;
				$t['name'] = $row -> name;
			}

			return $t;
		}
		return 0;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	######################################  поиск объявлений  ########################################
	
	
	
	
	
	
	
}

# http://www.ksoft.ru/opis_kladr.htm#_Toc137607893
